package com.tedu.jsd2407_ivos_fzj.user.service.impl;

import com.tedu.jsd2407_ivos_fzj.base.exception.ServiceException;
import com.tedu.jsd2407_ivos_fzj.base.response.JsonResult;
import com.tedu.jsd2407_ivos_fzj.base.response.StatusCode;
import com.tedu.jsd2407_ivos_fzj.user.mapper.UserMapper;
import com.tedu.jsd2407_ivos_fzj.user.pojo.dto.UserLoginParam;
import com.tedu.jsd2407_ivos_fzj.user.pojo.dto.UserQuery;
import com.tedu.jsd2407_ivos_fzj.user.pojo.dto.UserSaveParam;
import com.tedu.jsd2407_ivos_fzj.user.pojo.entity.User;
import com.tedu.jsd2407_ivos_fzj.user.pojo.vo.UserVO;
import com.tedu.jsd2407_ivos_fzj.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*业务层实现类需要添加@Service注解，可实现自动装配*/
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    /*重写：两同，两小，一大*/
    public UserVO login(UserLoginParam userLoginParam){
        log.debug("登录业务，参数:{}",userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if(userVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }
    public void saveUser(UserSaveParam userSaveParam){
        log.debug("新增/修改用户业务，参数：{}",userSaveParam);
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        if(userSaveParam.getId()==null){
            log.debug("新增用户业务，参数：{}",user);
            UserVO usernameVerify = userMapper.selectByUsername(userSaveParam.getUsername());
            if(usernameVerify!=null){
                throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
            }
            user.setPassword("123456");
            user.setCreateTime(new Date());
            if(userSaveParam.getStatus()==null||userSaveParam.getStatus().equals("")){
                user.setStatus("1");
            }
            log.debug("插入的user{}",user);
            userMapper.insert(user);
        }else{
            user.setUpdateTime(new Date());
            log.debug("修改用户业务，参数：{}",user);
            userMapper.update(user);
        }

    }

    public List<UserVO> selectUser(UserQuery userQuery){
        log.debug("查询用户业务，参数：{}",userQuery);
        List<UserVO> userVOList = userMapper.query(userQuery);
        return userVOList;
    }
    public void resetPassword(Long id){
        log.debug("重置密码业务,参数：{}",id);
        User user = new User();
        user.setId(id);
        user.setPassword("root");
        userMapper.update(user);
    }
    public void updateStatus(Long id,String status){
        log.debug("修改状态，参数{}，{}",id,status);
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Long id) {
        userMapper.deleteById(id);
    }

    @Override
    public List<UserVO> selectAuditList(Long id) {
        log.debug("查询审批人业务，参数：{}",id);
        ArrayList<UserVO> userVOList = new ArrayList<>();
        UserVO userVO1 = userMapper.selectAudit(id);
        userVOList.add(userVO1);
        if(userVO1.getParentId()!=null){
            UserVO userVO2 = userMapper.selectAudit(userVO1.getParentId());
            userVOList.add(userVO2);
        }
        return userVOList;
    }

}
